//
//  HLHomeFlowViewController.m
//  YueYue
//
//  Created by hulu001 on 2023/5/30.
//

#import "HLHomeFlowViewController.h"
#import "HLUserDetailViewController.h"
#import "HLHomeListRequest.h"
#import "HLHomeFlowCell.h"

@interface HLHomeFlowViewController ()

@property (nonatomic, strong) HLHomeLabelModel *label;

@end

@implementation HLHomeFlowViewController

- (instancetype)initWithLabel:(HLHomeLabelModel *)label;
{
    self = [super init];
    if (self){
        self.label = label;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)viewDidAppear:(BOOL)animated {
    if (!self.request){
        self.request = [[HLHomeListRequest alloc] initWithCode:_label.tagCode];
        self.request.delegate = self;
        [self requestInit];
    }
    [super viewDidAppear:animated];
}


- (void)requestInit
{
    [self showEmptyView];
    [self.emptyView.loadingView startAnimating];
    [self.emptyView setTextLabelText:@"加载中..."];
    [self.request start];
}

- (void)initCollectionView
{
    [super initCollectionView];
    
    CGFloat itemW = (SCREEN_WIDTH-28)/2.0;
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(itemW, itemW * 1.32);
    layout.minimumLineSpacing = 4.0;
    layout.minimumInteritemSpacing = 4.0;
    layout.sectionInset = UIEdgeInsetsMake(0, 12, 0, 12);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    [self setupMJRefresh];
    [self.collectionView registerClass:[HLHomeFlowCell class] forCellWithReuseIdentifier:[HLHomeFlowCell identifyCell]];
    [self.collectionView setCollectionViewLayout:layout];
    self.collectionView.backgroundColor = UIColorWhite;
    
}

- (void)layoutCollectionView
{
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(StatusBarHeightConstant + 42);
        make.left.bottom.right.mas_equalTo(0);
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.datasources.count;
}

- (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    HLHomeFlowCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[HLHomeFlowCell identifyCell] forIndexPath:indexPath];
    cell.model = self.datasources[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(HLHomeFlowCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    [cell animateWithIconWithIndexPath:indexPath];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    HLHomeListModel *user = self.datasources[indexPath.row];
    HLUserDetailViewController *userDetailVC = [[HLUserDetailViewController alloc] initWithUserId:user.userId];
    userDetailVC.homeModel = user;
    [self.navigationController pushViewController:userDetailVC animated:YES];
    MJWeakSelf
    [self.kvoController observe:user keyPath:@"chatUp" options:NSKeyValueObservingOptionNew block:^(id  _Nullable observer, id  _Nonnull object, NSDictionary<NSString *,id> * _Nonnull change) {
        NSString *changeString = change[NSKeyValueChangeNewKey];
        if ([changeString isEqualToString:@"1"]){
            [weakSelf.collectionView reloadItemsAtIndexPaths:@[indexPath]];
        }
    }];;
    
}


- (void)headerRefresh
{
    HLHomeListRequest *homeRequest = (HLHomeListRequest *)self.request;
    homeRequest.page = 1;
    [self.request start];
}

- (void)footerRefresh
{
    HLHomeListRequest *homeRequest = (HLHomeListRequest *)self.request;
    homeRequest.page += 1;
    [self.request start];
}

- (void)requestFinished:(__kindof YTKBaseRequest *)request
{
    [super requestFinished:request];
    
    HLHomeListRequest *homeRequest = (HLHomeListRequest *)request;
    
    if (homeRequest.page == 1){
        [self.datasources removeAllObjects];
    }
    
    NSArray *homeList = [homeRequest homeList];
    [self.datasources addObjectsFromArray:homeList];
    
    if (homeList.count < [HLListPageSize integerValue] && self.datasources.count > 0){
        [self noMordDataWithFooterRefresh];
    }
    
    if (self.datasources.count == 0){
        [self.emptyView setLoadingViewHidden:YES];
        [self.emptyView setImage:UIImageMake(@"empty_icon_2")];
        [self.emptyView setTextLabelText:@"空空如也"];
        [self.emptyView setDetailTextLabelText:[NSString stringWithFormat:@"哇哦，%@页面一个人也没有", _label.tagName]];
    } else {
        [self hideEmptyView];
    }
    
    [self.collectionView reloadData];
}

- (void)requestFailed:(__kindof YTKBaseRequest *)request
{
    [super requestFailed:request];
}

#pragma mark - JXCategoryListContentViewDelegate

/**
 实现 <JXCategoryListContentViewDelegate> 协议方法，返回该视图控制器所拥有的「视图」
 */
- (UIView *)listView {
    return self.view;
}


@end
